package cn.haoxsf.app.system.service.impl;

import cn.haoxsf.app.system.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @program: app
 * @description: redis操作Service的实现类
 * @author: Ele qvbanfm@qq.com
 * @create: 2020-01-09 14:17
 **/
@Service
public class RedisServiceImpl implements IRedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${jwt.expiration}")
    private Integer expirationSeconds;//jwt过期时间

    /**
     * 添加
     *
     * @param key
     * @param v
     */
    @Override
    public void setString(String key,String v) {
        stringRedisTemplate.opsForValue().set(String.format(TOKEN,key),v,expirationSeconds,TimeUnit.SECONDS);
    }

    /**
     * 添加
     *
     * @param key
     * @param v
     * @param time
     */
    @Override
    public void setString(String key, String v, Integer time) {
        stringRedisTemplate.opsForValue().set(String.format(TOKEN,key),v,time,TimeUnit.MINUTES);
    }

    /**
     * 获取
     *
     * @param key
     * @return
     */
    @Override
    public String getString(String key) {
        return stringRedisTemplate.opsForValue().get(String.format(TOKEN,key));
    }

    /**
     * 清除token
     *
     * @param token token
     */
    @Override
    public void logout(String token) {
        stringRedisTemplate.delete(String.format(TOKEN,token));
    }
}
